﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    public static partial class huifucontroller
    {

        /// <summary>

        /// </summary>
        /// <param name="pb"></param>
        //public static void huifu_chongzhi(pagebase pb)
        //{
            
        //}

        /// <summary>
        /// 后台手动放款，传递项目编号
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_fangkuan_zidong(pagebase pb)
        {
            //在返款操作之前，需要判断资金是否满标。满标后才能进行放款
            string proid = pb.v_ss("proid");
            //proid = CryptoHelper.Decrypt(proid);
            var list = supermodel.create("Mmanualbid").where("LogType='1' and proid=" + proid).list();

            //循环进行放款
            foreach (supermodel model in list)
            {
                huifu_fangkuang_model(model);
            }

            //全部放款成功才能更新状态
        }

        private static bool huifu_fangkuang_model(supermodel model)
        {
            Loans objsend = new Loans();

            //objsend.OrdId = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            //objsend.OrdDate = DateTime.Now.ToString("yyyyMMdd");

            //投资人 dr["BorrowerRate"]+
            objsend.OutCustId = model["UsrCustId"] + "";
            objsend.TransAmt = model["BorrowerAmt"] + "";
            objsend.Fee = HuiFuArg.LoansFee;//手续费


            //投标时订单号   需要跟投标一致
            objsend.SubOrdId = model["OrdId"] + "";
            objsend.SubOrdDate = model["OrdDate"] + "";
            objsend.InCustId = model["BorrowerCustId"] + "";
            objsend.DivDetails = "";
            //objsend.DivDetails = "[{\"DivCustId\":\"6000060000353790\",\"DivAcctId\":\"MDT000001\",\"DivAmt\":\"0.01\"}]";
            //objsend.DivCustId = "";
            //objsend.DivAcctId = "";
            //objsend.DivAmt = "";
            objsend.FeeObjFlag = "O";
            objsend.IsDefault = "N";
            objsend.IsUnFreeze = "Y";

            //解冻订单号，必须自己生成，后面不需要用到
            //我的数据库中，产生一个唯一标示。
            //objsend.UnFreezeOrdId = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            //这个必须是汇付返回的参数 //FreezeTrxId=201410200000242489

            //冻结标示。投标的时候返回的冻结标示
            objsend.FreezeTrxId = model["FreezeTrxId"] + "";
            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_fangkuan_zidong_returnbg";


            //进行调用之前，先产生流水  操作这个表Mautoappropriation 把里面的信息补充完整
            supermodel Mautoappropriation = supermodel.create("Mautoappropriation");
            //根据借款人客户号查询借款人用户ID
            Mautoappropriation["User_Id"] = core_user.getUsrCustId(model["BorrowerCustId"] + "");

            Mautoappropriation["OrdId"] = objsend.OrdId;//订单号
            Mautoappropriation["OutCustId"] = objsend.OutCustId;//出账客户单
            Mautoappropriation["TransAmt"] = objsend.TransAmt;//交易金额
            Mautoappropriation["SubOrdId"] = objsend.SubOrdId;//投标时订单号
            Mautoappropriation["InCustId"] = objsend.InCustId;//入账客户号
            Mautoappropriation["FeeObjFlag"] = objsend.FeeObjFlag;//续费收取对象标志I/O
            Mautoappropriation["IsDefault"] = objsend.IsDefault;//是否默认
            Mautoappropriation["IsUnFreeze"] = objsend.IsUnFreeze;//是否解冻
            Mautoappropriation["UnFreezeOrdId"] = objsend.UnFreezeOrdId;//解冻订单号
            Mautoappropriation["proid"] = model["proid"];//项目ID

            //LogType 0 代表未处理，1代表已经处理
            Mautoappropriation["LogType"] = "0";
            var ret = Mautoappropriation.saveorupdate();

            //把主键设置上
            objsend.MerPriv = ret.identity + "";

            objsend.ReqExt = "";
            objsend.VocherAmt = "";
            //objsend.ProId = "";


            var r = core_huifu.Send<RetLoans>(objsend);
            if (r.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                //返回原来投标记录的主键
                Mautoappropriation = supermodel.create("Mautoappropriation");
                Mautoappropriation["Mautoappropriationid"] = r.MerPriv + "";
                Mautoappropriation["FreezeTrxId"] = r.FreezeTrxId;//冻结标示
                Mautoappropriation["MerCustId"] = r.MerCustId;//客户号
                Mautoappropriation["Fee"] = r.Fee;//扣款手续费
                //代表放款成功
                Mautoappropriation["logtype"] = "1";
                //更新信息
                Mautoappropriation.saveorupdate();

                RetLoans objrev = new RetLoans();
                //后台调用，最终要打印出来参数
                core_huifu.ReturnUrl(HttpContext.Current, "OrdId", objrev, true);
                core_huifu.saveHuiFuLiuShui(objrev);

                //必须全部成功才能更新，这里有明显的错误！
                //放款成功更新标的状态
                //jiandansql.execute("update pjkjbxx set zhuangtai='10' where pjkjbxxid=" + proid);
                //jshelper.alertandback("放款成功！");

                return true;
            }
            else
            {
                return false;
                //jshelper.alertandback("放款失败！" + r.RespCode.tostr());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_fangkuan_orderid(pagebase pb)
        {
            //在返款操作之前，需要判断资金是否满标。满标后才能进行放款
            //todo 获取到所有投资该产品的投标记录，这些记录已经成功。
            var model = supermodel
                .create("Mmanualbid")
                .where("LogType='1' and OrdId='" + pb.v_ss("orderID") + "'").get();

            huifu_fangkuang_model(model);
        }

        /// <summary>
        /// 异步通知，是否成功。
        /// </summary>
        public static void huifu_fangkuan_zidong_returnbg(pagebase pb)
        {
            RetLoans objrev = new RetLoans();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "OrdId", objrev, true);
            core_huifu.saveHuiFuLiuShui(objrev);

            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                supermodel Mautoappropriation = supermodel.create("Mautoappropriation");
                //返回原来投标记录的主键
                Mautoappropriation["Mautoappropriationid"] = objrev.MerPriv + "";
                Mautoappropriation["FreezeTrxId"] = objrev.FreezeTrxId;//冻结标示
                //Mautoappropriation["TrxId"] = objrev.TrxId;
                Mautoappropriation["MerCustId"] = objrev.MerCustId;//客户号
                Mautoappropriation["Fee"] = objrev.Fee;//扣款手续费
                //代表放款成功
                Mautoappropriation["logtype"] = "1";
                //更新信息
                //lxwdbhelper.createorupdate(Mautoappropriation, "Mautoappropriationid");
                Mautoappropriation.saveorupdate();
                //jshelper.alertandback("放款成功！");
            }
            else
            {
                //jshelper.alertandback("放款失败！" + objrev.RespCode.tostr());
            }
        }
    }
}